﻿
using HBase;
using hcollection;
using hconnect;
using System.Collections.Generic;
namespace hCommand {
    public abstract class AbsSynchro :Synchro {
        static CoreTaskParamStatus[] wcsTaskStatus = new CoreTaskParamStatus[] { CoreTaskParamStatus.满放变更 ,CoreTaskParamStatus.空取 };
        protected short CraneId;

        public ICoreTaskServer coreTaskServer = WMSDalFactray.getDal<ICoreTaskServer>();
        public AbsSynchro(int craneId) {
            this.CraneId = ( short ) craneId;
        }

        protected override bool onlyOneTimeAsync() {

            time = 400;
            return true;
        }
        public virtual MyDict<int ,TaskStatusPersistence> getTaskNo(AbsStatus status) {
            return null;
        }
        protected override void syncDb(AbsStatus cs) {
            MyDict<int ,TaskStatusPersistence> dict = getTaskNo(cs);
            if( dict != null ) {
                updataDB(dict);
            }
        }
        bool isError(int hisStatus) {
            foreach( CoreTaskParamStatus s in wcsTaskStatus ) {
                if( hisStatus == ( int ) s ) {
                    return true;
                }
            }
            return false;

        }

        public virtual void updataDB(MyDict<int ,TaskStatusPersistence> dict) {
            foreach( KeyValuePair<int ,TaskStatusPersistence> kv in dict ) {
                int taskNo = kv.Key;
                if( taskNo == 0 ) {
                    continue;
                }

                TaskStatusPersistence tsp = kv.Value;
                CoreTaskParam param = MyCache.queryUnique<CoreTaskParam>((a) => {
                    return a.taskNo == taskNo && a.type != 9;
                } ,"CompleteParam 过多" + taskNo);
                if( param == null ) {
                    consoleOnly("无有效任务{1}在站台{0}。" ,tsp.stn ,taskNo);
                    continue;

                }
                int hisStatus = param.status;
                if( isError(hisStatus) ) {
                    consoleOnly("站台{0}的任务{1}状态异常" ,tsp.stn ,taskNo);
                    continue;
                }

                int upStatus = tsp.status;
                if( hisStatus < upStatus ) {
                    tsp.status = upStatus;

                    coreTaskServer.updateTaskStatus(tsp);//顺序不能变
                } else {
                    console(taskNo + "状态以完成:" + upStatus);
                }
                tsp.t.status = ( short ) tsp.status;


            }

        }





    }
}
